-- [yue]: Script\UI\Story.yue
local type = _G.type -- 1
local Class = Dora.Class -- 1
local property = Dora.property -- 1
local print = _G.print -- 1
local Sprite = Dora.Sprite -- 1
local thread = Dora.thread -- 1
local Cache = Dora.Cache -- 1
local tostring = _G.tostring -- 1
local sleep = Dora.sleep -- 1
local Path = Dora.Path -- 1
local View = Dora.View -- 1
local Pass = Dora.Pass -- 1
local SpriteEffect = Dora.SpriteEffect -- 1
local collectgarbage = _G.collectgarbage -- 1
local _module_0 = nil -- 1
local Story = require("UI.View.Story") -- 2
local StoryFigure = require("UI.StoryFigure") -- 3
local Answer = require("UI.Answer") -- 4
local Struct = require("Utils").Struct -- 5
local YarnRunner = require("YarnRunner") -- 6
local Config = require("Data.Config") -- 7
local Command = require("System.Command") -- 8
local Dialog = Struct.Story.Dialog("character", "name", "text") -- 10
local getCharName -- 12
getCharName = function(current) -- 12
	if current.marks then -- 16
		local _list_0 = current.marks -- 17
		for _index_0 = 1, #_list_0 do -- 17
			local mark = _list_0[_index_0] -- 17
			local _type_0 = type(mark) -- 18
			local _tab_0 = "table" == _type_0 or "userdata" == _type_0 -- 18
			if _tab_0 then -- 18
				local attr = mark.name -- 18
				local name -- 18
				do -- 18
					local _obj_0 = mark.attrs -- 18
					local _type_1 = type(_obj_0) -- 18
					if "table" == _type_1 or "userdata" == _type_1 then -- 18
						name = _obj_0.name -- 18
					end -- 20
				end -- 20
				local id -- 18
				do -- 18
					local _obj_0 = mark.attrs -- 18
					local _type_1 = type(_obj_0) -- 18
					if "table" == _type_1 or "userdata" == _type_1 then -- 18
						id = _obj_0.id -- 18
					end -- 20
				end -- 20
				if name == nil then -- 18
					name = '' -- 18
				end -- 18
				if id == nil then -- 18
					id = '' -- 18
				end -- 18
				if attr ~= nil then -- 18
					if ("char" == attr or "Character" == attr) then -- 19
						return name, id -- 20
					end -- 19
				end -- 18
			end -- 20
		end -- 20
	end -- 16
	return '', '' -- 21
end -- 12
_module_0 = Class(Story, { -- 24
	reviewVisible = property(function(self) -- 24
		return self._reviewVisible -- 24
	end, function(self, value) -- 25
		self._reviewVisible = value -- 26
		self.reviewMask.visible = value -- 27
		self.topCenter.visible = value -- 28
		self.reviewBack.visible = value -- 29
		self.reviewArea.visible = value -- 30
	end), -- 24
	advance = function(self, option) -- 32
		local action, result = self._runner:advance(option) -- 33
		if "Text" == action then -- 34
			if result.optionsFollowed then -- 35
				local _ -- 36
				_, self._options = self._runner:advance() -- 36
			else -- 38
				self._options = nil -- 38
			end -- 35
			self._current = result -- 39
			return true -- 40
		elseif "Command" == action then -- 41
			self._current = nil -- 42
			self._options = nil -- 43
			self.answerList:removeAllChildren() -- 44
			self.continueIcon.visible = true -- 45
			self._advancing = true -- 46
			return true -- 47
		elseif "Option" == action then -- 48
			self._options = result -- 49
			return true -- 50
		elseif "Error" == action then -- 51
			return print(result) -- 52
		else -- 54
			return false -- 54
		end -- 54
	end, -- 32
	__init = function(self, dialogFile) -- 56
		self:gslot("Command.Preload", function(items) -- 57
			self._preloads = items -- 57
		end) -- 57
		self:gslot("Command.Figure", function(filename, x, y, scale) -- 58
			self.figure:removeAllChildren() -- 59
			if not filename then -- 60
				self._dialog.character = "" -- 61
				return -- 62
			end -- 60
			local _with_0 = Sprite(filename) -- 63
			if _with_0 ~= nil then -- 63
				_with_0.x, _with_0.y = x, y -- 64
				_with_0.scaleX = scale -- 65
				_with_0.scaleY = scale -- 65
				_with_0:addTo(self.figure) -- 66
			end -- 63
			return _with_0 -- 63
		end) -- 58
		self._runner = YarnRunner(dialogFile, "Start", Config, Command) -- 67
		self:advance() -- 68
		self.reviewVisible = false -- 69
		self._advancing = false -- 70
		local nextSentence -- 71
		nextSentence = function() -- 71
			if self._advancing then -- 72
				return -- 72
			end -- 72
			if self._options then -- 73
				return -- 73
			end -- 73
			if self:advance() then -- 74
				return thread(function() -- 75
					return self:updateDialogAsync() -- 75
				end) -- 75
			else -- 77
				return self:hide() -- 77
			end -- 74
		end -- 71
		self:gslot("Story.Advance", function() -- 78
			self._advancing = false -- 79
			return nextSentence() -- 80
		end) -- 78
		self.confirm:onKeyDown(function(keyName) -- 81
			if keyName == "Return" then -- 81
				return nextSentence() -- 81
			end -- 81
		end) -- 81
		self.confirm:slot("Tapped", nextSentence) -- 82
		self.textArea:slot("NoneScrollTapped", nextSentence) -- 83
		self.reviewButton:slot("Tapped", function() -- 84
			if self._advancing then -- 85
				return -- 85
			end -- 85
			self.reviewVisible = true -- 86
			return self:alignLayout() -- 87
		end) -- 84
		self.reviewBack:slot("Tapped", function() -- 88
			self.reviewVisible = false -- 88
		end) -- 88
		self.reviewArea:slot("NoneScrollTapped", function() -- 89
			self.reviewVisible = false -- 89
		end) -- 89
		do -- 90
			local _with_0 = Dialog() -- 90
			_with_0.__modified = function(key, value) -- 91
				if "character" == key then -- 92
					if (value ~= nil) and value ~= "" then -- 93
						self.figure:removeAllChildren() -- 94
						return self.figure:addChild(StoryFigure({ -- 95
							char = value -- 95
						})) -- 95
					end -- 93
				elseif "name" == key then -- 96
					if (value ~= nil) then -- 97
						self.name.text = value -- 97
					end -- 97
				elseif "text" == key then -- 98
					if (value ~= nil) then -- 99
						self.text.text = value -- 99
					end -- 99
				end -- 99
			end -- 91
			_with_0.__updated = function() -- 100
				return self:alignLayout() -- 100
			end -- 100
			self._dialog = _with_0 -- 90
		end -- 90
		self._reviews = { } -- 101
		self.visible = false -- 102
	end, -- 56
	updateDialogAsync = function(self) -- 104
		if not self._current then -- 105
			return -- 105
		end -- 105
		if self._advancing then -- 106
			return -- 106
		end -- 106
		self._advancing = true -- 107
		local name, characterId = getCharName(self._current) -- 108
		if characterId == 'vivi' then -- 109
			Cache:loadAsync("spine:vikaFigure") -- 110
			self._dialog.character = characterId -- 111
		elseif characterId ~= '' then -- 112
			Cache:loadAsync("spine:" .. tostring(characterId) .. "Figure") -- 113
			self._dialog.character = characterId -- 114
		end -- 109
		local text = self._current.text -- 115
		do -- 116
			local _obj_0 = self._reviews -- 116
			_obj_0[#_obj_0 + 1] = { -- 116
				name = name, -- 116
				text = text -- 116
			} -- 116
		end -- 116
		self._dialog.name = name -- 117
		self._dialog.text = text -- 118
		self.answerList:removeAllChildren() -- 119
		if self._options then -- 120
			self.continueIcon.visible = false -- 121
			local count = #self._options -- 122
			for i = 1, count do -- 123
				local option = self._options[i] -- 124
				if type(option) ~= "table" then -- 126
					print("skip option index=", i, "type=", type(option), "value=", tostring(option)) -- 127
					goto _continue_0 -- 128
				end -- 126
				name, characterId = getCharName(option) -- 130
				local optionText = option.text -- 131
				self.answerList:addChild((function() -- 133
					local _with_0 = Answer({ -- 133
						text = optionText -- 133
					}) -- 133
					_with_0:slot("Tapped", function() -- 134
						_with_0.touchEnabled = false -- 135
						return thread(function() -- 136
							sleep(0.3) -- 137
							do -- 138
								local _obj_0 = self._reviews -- 138
								_obj_0[#_obj_0 + 1] = { -- 138
									name = name, -- 138
									text = optionText -- 138
								} -- 138
							end -- 138
							if self:advance(i) then -- 139
								return thread(function() -- 140
									return self:updateDialogAsync() -- 140
								end) -- 140
							else -- 142
								return self:hide() -- 142
							end -- 139
						end) -- 142
					end) -- 134
					return _with_0 -- 133
				end)()) -- 133
				local size = self.answerList:alignItems(40) -- 143
				self.answerList.size = size -- 144
				self.answerList:alignItems(40) -- 145
				::_continue_0:: -- 124
			end -- 145
		else -- 147
			self.continueIcon.visible = true -- 147
		end -- 120
		self._advancing = false -- 148
		if self._preloads then -- 149
			local _list_0 = self._preloads -- 150
			for _index_0 = 1, #_list_0 do -- 150
				local item = _list_0[_index_0] -- 150
				if "" == Path:getExt(item) then -- 151
					local figureFile -- 152
					if 'vivi' == item then -- 153
						figureFile = "spine:vikaFigure" -- 153
					else -- 154
						figureFile = "spine:" .. tostring(item) .. "Figure" -- 154
					end -- 154
					Cache:loadAsync(figureFile) -- 155
				else -- 157
					Cache:loadAsync(item) -- 157
				end -- 151
			end -- 157
			self._preloads = nil -- 158
		end -- 149
	end, -- 104
	showAsync = function(self) -- 160
		self:updateDialogAsync() -- 161
		self.visible = true -- 162
		self._viewScale = View.scale -- 163
		self._viewEffect = View.postEffect -- 164
		View.scale = 4 * self._viewScale -- 165
		local size = View.size -- 166
		local blurH -- 167
		do -- 167
			local _with_0 = Pass("builtin:vs_sprite", "builtin:fs_spriteblurh") -- 167
			_with_0.grabPass = true -- 168
			_with_0:set("u_radius", size.width) -- 169
			blurH = _with_0 -- 167
		end -- 167
		local blurV -- 170
		do -- 170
			local _with_0 = Pass("builtin:vs_sprite", "builtin:fs_spriteblurv") -- 170
			_with_0.grabPass = true -- 171
			_with_0:set("u_radius", size.height) -- 172
			blurV = _with_0 -- 170
		end -- 170
		do -- 173
			local _with_0 = SpriteEffect() -- 173
			for _ = 1, 3 do -- 174
				_with_0:add(blurH) -- 175
				_with_0:add(blurV) -- 176
			end -- 176
			View.postEffect = _with_0 -- 173
		end -- 173
		return self:gslot("AppChange", function(settingName) -- 177
			if settingName == "Size" then -- 177
				local width, height -- 178
				do -- 178
					local _obj_0 = View.size -- 178
					width, height = _obj_0.width, _obj_0.height -- 178
				end -- 178
				blurH:set("u_radius", width) -- 179
				return blurV:set("u_radius", height) -- 180
			end -- 177
		end) -- 180
	end, -- 160
	hide = function(self) -- 182
		self:gslot("AppChange", nil) -- 183
		self:emit("Ended") -- 184
		self:removeFromParent() -- 185
		local viewScale = self._viewScale -- 186
		local viewEffect = self._viewEffect -- 187
		return thread(function() -- 188
			collectgarbage() -- 189
			Cache:removeUnused() -- 190
			View.scale = viewScale -- 191
			sleep() -- 192
			View.postEffect = viewEffect -- 193
		end) -- 193
	end -- 182
}) -- 23
return _module_0 -- 193
